"""
---------------------------------------------------------------------
- File: response.py
- Date: 2025/11/12
- Author: yangzhide
- Email: xxzjyzd@126.com
- Description: 统一响应处理
---------------------------------------------------------------------
"""

from typing import Generic, TypeVar, Any

from pydantic import BaseModel, Field

T = TypeVar("T")


class ResponseBase(BaseModel, Generic[T]):
    """
    统一的API响应格式
    """

    code: int = Field(200, description="响应状态码")
    message: Any = Field("success", description="响应消息")
    data: T | None = Field(None, description="响应数据")


class ResponseSuccess(ResponseBase):
    """
    成功的API响应格式, 只需传递data即可
    """

    code: int = 200
    message: str = "success"


class ResponseError(ResponseBase):
    """
    失败的API响应格式， 需传递message和code
    """

    code: int = Field(500, description="响应状态码")
    message: Any = Field("error", description="响应消息")
    data: None = None


# 验证失败的响应格式
class ResponseValidationError(ResponseBase):
    """
    验证失败
    """

    code: int = Field(422, description="响应状态码")
    message: Any = Field("数据校验失败详情...", description="响应消息")
    data: None = None


# 请求参数校验统一响应格式
standard_response = {422: {"model": ResponseValidationError, "description": "请求参数校验失败"}}
